package com.atbeijing.write;

import com.atbeijing.bean.Teacher;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Bulk;
import io.searchbox.core.Index;

import java.io.IOException;

/**
 * 批量写入es
 */
public class WriteBulk {
    public static void main(String[] args) throws IOException {
        //1.获取jest客户端工厂
        JestClientFactory jestClientFactory = new JestClientFactory();

        //2.设置连接ES地址
        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop202:9200").build();
        jestClientFactory.setHttpClientConfig(httpClientConfig);

        //3.获取连接
        JestClient jestClient = jestClientFactory.getObject();

        //4.批量写入数据
        Teacher t1 = new Teacher(1L,"王五1",19,"男","洗头,泡澡,足浴,骑行q");
        Teacher t2 = new Teacher(2L,"王五2",119,"男","洗头,泡澡,足浴,骑行");
        Teacher t3 = new Teacher(3L,"王五3",1119,"男","洗头,泡澡,足浴,骑行");

        Index id1 = new Index.Builder(t1).id("ww1").build();
        Index id2 = new Index.Builder(t2).id("ww2").build();
        Index id3 = new Index.Builder(t3).id("ww3").build();

        Bulk build = new Bulk.Builder()
                .defaultIndex("teacher_wangwu")
                .defaultType("_doc")
                .addAction(id1)
                .addAction(id2)
                .addAction(id3)
                .build();

        jestClient.execute(build);

        //关闭连接
        jestClient.shutdownClient();
    }
}
